package com.example.dnf.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @version: V2.0.0
 * @author: hq
 * @className: PublicController
 * @packageName: com.example.dnf.controller
 * @description:
 * @date: 2024/6/5 17:17
 */
@RestController
@RequestMapping("/pub")
public class PublicController {
    @GetMapping("/test")
    public String test(@RequestParam String str){
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + " : hello : " + str;
    }

//    public static void main(String[] args) throws ExecutionException, InterruptedException {
//
//        ExecutorService executor = Executors.newFixedThreadPool(10);
//        for (int i = 0; i < 10; i++) {
//            a(executor);
//        }
//
//        System.out.println("12222222222");
//    }
//    public static void a(ExecutorService executor) throws ExecutionException, InterruptedException {
//        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
//            // 长时间运行的任务
//            System.out.println(Thread.currentThread().getName());
//            try {
//                Thread.sleep(3000);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//            return "Result";
//        }, executor);
////        future.get();
//    }

    private static final int MAX = 100;
    private static int number = 1;
    private static final Object lock = new Object();

    public static void main(String[] args) {
        // 奇数线程
        Thread oddThread = new Thread(() -> {
            while (number <= MAX) {
                synchronized (lock) {
                    while (number % 2 == 0) { // 如果是偶数，等待
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    if (number <= MAX) {
                        System.out.println("奇数线程: " + number);
                        number++;
                    }
                    lock.notifyAll();
                }
            }
        });

        // 偶数线程
        Thread evenThread = new Thread(() -> {
            while (number <= MAX) {
                synchronized (lock) {
                    while (number % 2 != 0) { // 如果是奇数，等待
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    if (number <= MAX) {
                        System.out.println("偶数线程: " + number);
                        number++;
                    }
                    lock.notifyAll();
                }
            }
        });

        oddThread.start();
        evenThread.start();
    }

}
